package com.education.welco.controller;

import com.education.common.pojo.AjaxResult;
import com.education.welco.pojo.SysSensitiveWord;
import com.education.welco.service.ISysSensitiveWordService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/sysSensitiveWord")
public class SysSensitiveWordController {

    @Autowired
    private ISysSensitiveWordService sysSensitiveWordService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    /**
     * 导入屏蔽词
     */
    /*@PostMapping(value = "/importSensitiveWord")
    public AjaxResult importSensitiveWord(@RequestParam("file") MultipartFile file){
        try {
            List<String[]> list = ReadExcelUtils.readExcel(file);
            StringBuilder stringBuilder = new StringBuilder();
            for (String[] strings : list) {
                String value = strings[1];
                stringBuilder.append(value).append(",");
                SysSensitiveWord sysSensitiveWord = new SysSensitiveWord();
                sysSensitiveWord.setSensitiveWord(value);
                sysSensitiveWord.settCreateTime(new Date());
                sysSensitiveWord.setSensitiveTimes(1);
                sysSensitiveWord.settUpdateTime(new Date());
                sysSensitiveWordService.addSensitiveWord(sysSensitiveWord);
            }
            String s = stringBuilder.toString();
            redisTemplate.opsForValue().set("system:sensitiveWord", redisTemplate.opsForValue().get("system:sensitiveWord")+ "," + s);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return AjaxResult.success("添加成功");
    }*/


    @PostMapping(value = "/addSensitiveWord")
    @ApiOperation("添加屏蔽词")
    public AjaxResult addSensitiveWord(@RequestBody SysSensitiveWord sysSensitiveWord) {
        String words = sysSensitiveWord.getSensitiveWord();
        sysSensitiveWord.settCreateTime(new Date());
        if (sysSensitiveWordService.addSensitiveWord(sysSensitiveWord) > 0) {
            if (redisTemplate.hasKey("system:sensitiveWord")) {
                redisTemplate.opsForValue().set("system:sensitiveWord", redisTemplate.opsForValue().get("system:sensitiveWord") + "," + words);
            } else {
                redisTemplate.opsForValue().set("system:sensitiveWord", words);
            }
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("该词已存在，请勿重复添加！");
        }

        /*String[] wordsArr = words.split(",");
        if (wordsArr.length > 0) {
            for (String s : wordsArr) {
                SysSensitiveWord sw = new SysSensitiveWord();
                sw.setSensitiveWord(s);
                sw.settCreateTime(new Date());
                if (sysSensitiveWordService.addSensitiveWord(sw) > 0) {
                    if (redisTemplate.hasKey("system:sensitiveWord")) {
                        redisTemplate.opsForValue().set("system:sensitiveWord", redisTemplate.opsForValue().get("system:sensitiveWord") + "," + s);
                    } else {
                        redisTemplate.opsForValue().set("system:sensitiveWord", words);
                    }
                }
            }
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败");
        }*/

    }


    @PostMapping(value = "/updateSensitiveWord")
    @ApiOperation("修改屏蔽词")
    public AjaxResult updateSensitiveWord(@RequestBody SysSensitiveWord sysSensitiveWord) {
        if (sysSensitiveWordService.updateSensitiveWord(sysSensitiveWord) > 0) {
            List<SysSensitiveWord> sysSensitiveWordList = sysSensitiveWordService.getSysSensitiveWordAllList();
            if (sysSensitiveWordList.size() > 0) {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < sysSensitiveWordList.size(); i++) {
                    if (i == sysSensitiveWordList.size() - 1) {
                        sb.append(sysSensitiveWordList.get(i).getSensitiveWord());
                    } else {
                        sb.append(sysSensitiveWordList.get(i).getSensitiveWord()).append(",");
                    }
                }
                redisTemplate.opsForValue().set("system:sensitiveWord", sb.toString());
            }
            return AjaxResult.success("修改成功");
        } else {
            return AjaxResult.error("修改失败");
        }
    }


    @PostMapping(value = "/delSensitiveWord")
    @ApiOperation("删除屏蔽词")
    public AjaxResult delSensitiveWord(@RequestBody SysSensitiveWord sysSensitiveWord) {
        if (sysSensitiveWordService.delSensitiveWord(sysSensitiveWord) > 0) {
            List<SysSensitiveWord> sysSensitiveWordList = sysSensitiveWordService.getSysSensitiveWordAllList();
            if (sysSensitiveWordList.size() > 0) {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < sysSensitiveWordList.size(); i++) {
                    if (i == sysSensitiveWordList.size() - 1) {
                        sb.append(sysSensitiveWordList.get(i).getSensitiveWord());
                    } else {
                        sb.append(sysSensitiveWordList.get(i).getSensitiveWord()).append(",");
                    }
                }
                redisTemplate.opsForValue().set("system:sensitiveWord", sb.toString());
            } else {
                if (redisTemplate.hasKey("system:sensitiveWord")) {
                    redisTemplate.delete("system:sensitiveWord");//删除掉redis中的用户数据
                }
            }
            return AjaxResult.success("删除成功");
        } else {
            return AjaxResult.error("删除失败");
        }
    }


    @PostMapping(value = "/getSysSensitiveWordList")
    @ApiOperation("屏蔽词列表")
    public AjaxResult getSysSensitiveWordList(@RequestBody SysSensitiveWord sysSensitiveWord) {
        PageInfo pageInfo = sysSensitiveWordService.getSysSensitiveWordList(sysSensitiveWord);
        return AjaxResult.success("查询成功", pageInfo);
    }
}
